<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // ** 1. 将数组中数字内容求和  for forEach reduce 分别写出
        let arr = [2, 6, 7, 1, 20, 34]

        // for
        let sum = 0
        for (let i = 0; i < arr.length; i++) {
            sum += arr[i]
        }
        console.log(sum)

        sum = 0
        arr.forEach(item => {
            sum += item
        })
        console.log(sum)

        //reduce
        sum = arr.reduce((prev, current) => prev + current, 0)
        console.log(sum)

        // ** 2. 计算obj里面每一个对象nums的总和，并设置成对象里面的total属性
        //       计算所有nums总和
        var obj = [{
            nums: [2, 3, 4, 5]
        }, {
            nums: [12, 3, 24, 15]
        }, {
            nums: [22, 3, 14, 5]
        }, {
            nums: [12, 23, 24, 15]
        }, ];

        let allTotal = 0
        let newObj = obj.map(item => {

            let total = item.nums.reduce((prev, current) => prev + current, 0)

            allTotal += total //计算全总和

            return {
                nums: item.nums,
                total
            }
        })

        console.log(newObj, allTotal)
    </script>
</body>

</html>